//
//  SlideCollectionReusableView.m
//  projectA_kuaiZuiManHua
//
//  Created by 张祎 on 15-3-22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SlideCollectionReusableView.h"
#import "SlideCollectionViewCell.h"
@interface SlideCollectionReusableView () <UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, assign) NSInteger page;
@end

@implementation SlideCollectionReusableView
- (void)dealloc
{
    [_slideCollectionView release];
    [_slidePageControl release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        self.slideCollectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
        _slideCollectionView.backgroundColor = [UIColor brownColor];
        _slideCollectionView.pagingEnabled = YES;
        _slideCollectionView.bounces = NO;
        [_slideCollectionView registerClass:[SlideCollectionViewCell class] forCellWithReuseIdentifier:@"h"];
        _slideCollectionView.delegate = self;
        _slideCollectionView.dataSource = self;
        [self addSubview:_slideCollectionView];
        [_slideCollectionView release];
        
        //幻灯片页面按钮
        self.slidePageControl = [[UIPageControl alloc]init];
        [self addSubview:_slidePageControl];
        [_slidePageControl release];
        
    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SlideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"h" forIndexPath:indexPath];
    
    cell.galleryItem = [_galleryItems objectAtIndex:indexPath.row];
    
    return cell;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    CGFloat width = layoutAttributes.size.width;
    CGFloat height = layoutAttributes.size.height;
    _slidePageControl.frame = CGRectMake(width * 4 / 5 - 20, height * 4 / 5 + 20, 100, 20);
    _slidePageControl.numberOfPages = 4;
    [_slidePageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    _slidePageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _slidePageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    self.backgroundColor = [UIColor blueColor];
    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer
{
    NSLog(@"1");
    [_slideCollectionView setContentOffset:CGPointMake(_page * self.bounds.size.width, 0) animated:YES];
    _slidePageControl.currentPage = _page;
    _page++;
    if (_page == 4) {
        _page = 0;
    }
    
}
- (void)pageAction:(UIPageControl *)page
{
    [_slideCollectionView setContentOffset:CGPointMake(page.currentPage * self.bounds.size.width, 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _slidePageControl.currentPage = _slideCollectionView.contentOffset.x / self.bounds.size.width;
}

@end
